package com.city.itis.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.city.itis.dao.SiteCategoryDao;
import com.city.itis.domain.SiteCategory;


/**
 * 站点类别Dao层实现类
 * @author WY
 *
 */

@Component("siteCategoryDao")
public class SiteCategoryDaoImpl implements SiteCategoryDao {
	
	private HibernateTemplate hibernateTemplate;
	
    
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
    @Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public int save(SiteCategory siteCategory) {
		Integer flag=(Integer)this.hibernateTemplate.save(siteCategory);	//调用保存方法
		return flag;
	}

	@Override
	public int delete(SiteCategory siteCategory) {
		try
		{
			this.hibernateTemplate.delete(siteCategory);	//调用删除方法
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0; //删除失败
		}
		return 1; //删除成功
	}

	@Override
	public int deleteById(Integer id) {
		SiteCategory siteCategory =(SiteCategory)this.hibernateTemplate.get(SiteCategory.class, id); //从数据库中取得用于系想你
		return delete(siteCategory); //调用delete方法
	}

	@Override
	public int update(SiteCategory siteCategory) 
	{
		try{
		this.hibernateTemplate.update(siteCategory);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;	//修改失败
		}
		return 1;	//修改成功
	}

	@Override
	public SiteCategory getSiteCategoryById(Integer id) {
		SiteCategory siteCategory=(SiteCategory)this.hibernateTemplate.get(SiteCategory.class, id); 
		if(siteCategory!=null)
		{
			return siteCategory;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public SiteCategory getSiteCategoryBySiteCategoryName(String categoryName) 
	{
		String queryString="FROM SiteCategory sc where sc.categoryName=?";
		List<SiteCategory> siteCategoryList=(List<SiteCategory>)this.hibernateTemplate.find(queryString,categoryName);
		if(siteCategoryList!=null)
		{
			for(SiteCategory siteCategory :siteCategoryList)
			return siteCategory;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SiteCategory> findAll() {
		String queryString="FROM SiteCategory sc  order by sc.categoryNo desc";
		List<SiteCategory> siteCategoryList=this.hibernateTemplate.find(queryString);
		if(siteCategoryList!=null)
		{
			return siteCategoryList;
		}
		return null;
	}
	

}
